﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Darku.PatternLock;
using System.Windows.Media.Imaging;

namespace PatternLockExample
{
    public partial class MainPage : PhoneApplicationPage
    {
        //Declare the PatternLockDialog
        PatternLockDialog patternLockDialog;

        string pattern;

        //The App Title would be displayed on the Lock Dialog
        string appTitle = "Darku.PatternLock Example";

        //This would indicate whether to set the Lock Dialog to a 3x3 or 4x4 matrix
        PatternLockMatrix matrix;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            // Sets the matrix to 3x3
            rd3.IsChecked = true;

            pattern = "";
            matrix = PatternLockMatrix.Three;
            
        }

        //This function initialises the properties of the pattern lock dialog
        private void InitPatternLock()
        {
            //Initialising the patternLockDialog instance
            patternLockDialog = new PatternLockDialog(appTitle, matrix);

            //Uncomment this to set Maximum Tries to 2 instead of the default 3
            //patternLockDialog = new PatternLockDialog(appTitle, 2 , matrix); 
            //patternLockDialog.MaxTries = 2;
            
            //Option to display background image. You can ignore to use default background
            if (BackgroundCheckBox.IsChecked == true)
            {
                string imageUri="./Images/bg.png";
                //Uncomment this to use a different image as the Background Image
                //imageUri="./Images/bg1.png";
                patternLockDialog.BackgroundImage = new BitmapImage(new Uri(imageUri, UriKind.RelativeOrAbsolute));
            }
            else
            {
                patternLockDialog.BackgroundImage = null;
            }

            //Uncomment this to see the customisable colors of the matrix

            //patternLockDialog.InnerBorderBackground = new SolidColorBrush(Colors.Magenta);
            //patternLockDialog.InnerBorderBrush = new SolidColorBrush(Colors.Transparent);

            //patternLockDialog.InnerBorderBackgroundClicked = new SolidColorBrush(Color.FromArgb(255, 82, 255, 47));
            //patternLockDialog.InnerBorderBrushClicked = new SolidColorBrush(Color.FromArgb(255, 82, 255, 47));

            //patternLockDialog.OuterBorderBackgroundClicked = new SolidColorBrush(Color.FromArgb(255, 82, 255, 47));
            //patternLockDialog.OuterBorderBrushClicked = new SolidColorBrush(Color.FromArgb(255, 82, 255, 47));


            //Declaring the dialog closed event to be triggered
            patternLockDialog.Closed += new EventHandler(dialog_Closed);
            //Declaring the dialog closed event to be triggered
            patternLockDialog.MaximumTries += new EventHandler(patternLockDialog_MaximumTries);
        }

        //The close dialog event of the Lock Dialog
        void dialog_Closed(object sender, EventArgs e)
        {
            // The pattern chosen from the Lock Dialog can then be retrieved from the Password
            // property of the Lock dialog and stored for use later
            pattern = patternLockDialog.Password;
        }

        //The MaximumTries event of the Lock Dialog
        void patternLockDialog_MaximumTries(object sender, EventArgs e)
        {
            MessageBox.Show("Maximum tries reached","Maximum Tries",MessageBoxButton.OK);
        }

        private void btnChoosePattern_Click(object sender, RoutedEventArgs e)
        {
            //Intialise the properties of the Lock Dialog
            InitPatternLock();

            //The show method is use to collect the pattern from the user for the first time
            patternLockDialog.Show();
        }

        private void btnCheckPattern_Click(object sender, RoutedEventArgs e)
        {
            //If a pattern has already been chosen the compare dialog is opened
            if (pattern != "")
            {
                InitPatternLock();

                // The show method passing the chosen pattern to it is use to compare
                // the pattern the user chose for the first time
                patternLockDialog.Show(pattern);
            }
            else
            { MessageBox.Show("Please choose a pattern"); }
        }

        private void RadioButtonForThreeMatrix_Checked(object sender, RoutedEventArgs e)
        {
            // Sets the matrix to 3x3
            matrix = PatternLockMatrix.Three;
        }

        private void RadioButtonForFourMatrix_Checked(object sender, RoutedEventArgs e)
        {
            // Sets the matrix to 4x4
            matrix = PatternLockMatrix.Four;
        }
    }
}